package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.BaseOilCardService;
import com.cloudlead.commonData.service.TbFuelRechargeService;
import com.cloudlead.commonData.service.TbFuelRegisterService;
import com.cloudlead.commonData.vo.BaseDriver;
import com.cloudlead.commonData.vo.BaseOilCard;
import com.cloudlead.commonData.vo.TbFuelRecharge;
import com.cloudlead.commonData.vo.TbFuelRegister;
import com.cloudlead.fleetAction.service.FuelRechargeSer;
import com.cloudlead.fleetAction.service.FuelRegisterSer;
import com.cloudlead.fleetService.vo.FuelRechargeVo;
import com.cloudlead.fleetService.vo.FuelRegisterVo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Jinnoo on 2015/8/12.
 */
@Service
public class FuelRegisterSerImpl implements FuelRegisterSer {

    @Autowired
    TbFuelRegisterService tbFuelRegisterService;
    @Autowired
    BaseOilCardService baseOilCardService;

    @Override
    public Page<FuelRegisterVo> findAllFuelRegister(FuelRegisterVo vo, int page, int rows) {
        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();
        List<Term> tr_1=new ArrayList<Term>();
        //查询条件
        tr.add(new Term("flag","0"));//未删除
        tr_1.add(new Term("baseCompany.id",vo.getBaseCompany()));//公司Id

        if(vo.getOilCardNumber()!=null&&!"".equals(vo.getOilCardNumber())){//油卡编号
            tr_1.add ((new Term("accountNumber",Operator.LK,vo.getOilCardNumber())));
        }

        //查询符合条件的油卡
        List<BaseOilCard> baseOilCards=baseOilCardService.list(new Filter(new Criteria(tr_1)),new Sort());
        List baseOilCardsNew = new ArrayList();
        for(BaseOilCard baseOilCard: baseOilCards){
            baseOilCardsNew.add(baseOilCard.getId());
        }

        //查询的油卡记录
        if (baseOilCardsNew.size()>0){

            tr.add ((new Term("baseOilCard.id",Operator.IN,baseOilCardsNew)));
        }

        if(vo.getStatus()!=null&&!"".equals(vo.getStatus()))//油卡领用记录状态
            tr.add ((new Term("status",vo.getStatus())));

        if (vo.getDateGet()!=null&&!"".equals(vo.getDateGet()))//领用日期
            tr.add ((new Term("dateGet",Operator.GE,vo.getDateGet())));

        if (vo.getBackDate()!=null&&!"".equals(vo.getBackDate()))//还卡日期
            tr.add ((new Term("backDate",Operator.GE,vo.getBackDate())));

        if (vo.getFleet()!=null&&!"".equals(vo.getFleet()))//车队
            tr.add ((new Term("fleet",vo.getFleet())));

        if (vo.getBaseDriver()!=null&&!"".equals(vo.getBaseDriver()))//驾驶员
            tr.add ((new Term("baseDriver.id",vo.getBaseDriver())));


        Filter filter=new Filter( new Criteria( tr));
        Sort sort = new Sort();
        if ("领用".equals(vo.getStatus())){
            sort.add(new Sort.Order( Sort.Direction.DESC ,"dateGet"));
        }else if ("已归还".equals(vo.getStatus())){
            sort.add(new Sort.Order( Sort.Direction.DESC ,"backDate"));
        }






        pr.setSort(sort);
        pr.setSize(rows);
        pr.setPage(page);
        pr.setFilter(filter);


        Page<TbFuelRegister> pb=(Page<TbFuelRegister>)tbFuelRegisterService.findAll(pr);
        Page<FuelRegisterVo> pb1=new Page<FuelRegisterVo>();
        BeanUtils.copyProperties(pb, pb1);
        return pb1;
    }

    @Override
    public void createFuelRegister(FuelRegisterVo vo) {
        TbFuelRegister register=new TbFuelRegister();
        BeanUtils.copyProperties(vo, register);
        register.setDateGet(new DateTime());//领用日期
        //油卡状态改为已领用



        register.setFlag("0");

        if(vo.getBaseOilCard()!=null && !"".equals(vo.getBaseOilCard())){//油卡记录
            BaseOilCard baseOilCard=baseOilCardService.view(vo.getBaseOilCard());
            register.setBaseOilCard(baseOilCard);//油卡编号

            baseOilCard.setStatus("已领用");
            baseOilCardService.update(baseOilCard);
        }
        if (vo.getBaseDriver()!=null&&!"".equals(vo.getBaseDriver())){//司机
            BaseDriver baseDriver=new BaseDriver();
            baseDriver.setId(vo.getBaseDriver());
            register.setBaseDriver(baseDriver);
        }

        tbFuelRegisterService.create(register);
    }

    @Override
    public void editFuelRegister(FuelRegisterVo vo) {
        TbFuelRegister register=tbFuelRegisterService.view(vo.getId());
        if("领用".equals(register.getStatus())){
            register.setStatus("已归还");
            //油卡状态改为待用
            register.getBaseOilCard().setStatus("待用");
            register.getBaseOilCard().setEndMoney((float)0);
            baseOilCardService.update(register.getBaseOilCard());
            register.setBackDate(new DateTime());//归还日期
            tbFuelRegisterService.update(register);
        }

    }

    @Override
    public void removenFuelRegister(FuelRegisterVo vo) {
        TbFuelRegister register=tbFuelRegisterService.view(vo.getId());
        register.setFlag("1");
        //油卡状态改为待用
        register.getBaseOilCard().setStatus("待用");
        baseOilCardService.update(register.getBaseOilCard());
        tbFuelRegisterService.update(register);

    }
}
